package com.caration.shell;

import android.app.Activity;
import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Display;

import com.caration.shell.config.CommonConfig;
import com.caration.shell.cr3d.RenderRunnable;
import com.caration.shell.cr3d.SharedData;
import com.caration.shell.workspace3D.ShellRenderHost;

public class ShellWallpaperManager extends BroadcastReceiver {

	public static int SCROLL_MODE = 0;
	public static int CENTER_MODE = 1;
	private static float c = 0.0F;
	private static int mSpeed = 0;

	private static ShellWallpaperManager instance;
	// private dc e;
	private WallpaperManager mWallpaperManager;
	private int h;
	private int i;
	private int width;
	private int height;
	private int l;
	private boolean m = true;
	private float n;
	private float o;
	private boolean p = false;

	public static ShellWallpaperManager instance() {
		if (instance == null)
			instance = new ShellWallpaperManager();
		return instance;
	}

	private void a(float xOffset, boolean paramBoolean) {
		if ((CommonConfig.getWallpaperMode() == SCROLL_MODE) || (paramBoolean)) {
			if (xOffset >= 0.0F) {
				if (xOffset > 1.0F)
					xOffset = 1.0F;
			} else
				xOffset = 0.0F;
			this.n = xOffset;
			this.o = xOffset;
			if (!CommonConfig.isLiveWallpaper()) {
				if (width < this.h) {
					float f1 = (this.h - width) / 2.0F;
					// this.e.H().x = (int) (f1 - xOffset * (f1 * 2.0F));
				} else {
					// this.e.H().x = 0.0F;
				}
				// PositionNumber3d localPositionNumber3d = this.e.H();
				int i1;
				if (this.i <= height)
					i1 = this.i / 2;
				else
					i1 = (this.i - height) / 2 + height / 2;
				// localPositionNumber3d.y = i1;
			} else {
				mWallpaperManager.setWallpaperOffsets(Home.getInstance()
						.getWorkspace().getWindowToken(), xOffset, 0.0F);
				mWallpaperManager.setWallpaperOffsetSteps(xOffset, 0.0F);
			}
		}
	}

	public static void setSpeed(int speed) {
		if (speed >= 0) {
			if (speed > 5)
				speed = 5;
		} else
			speed = 0;
		mSpeed = speed;
		c = 0.05F + 0.25F * (speed / 5.0F);
	}

	public static boolean isLiveWallpaper(Activity context) {
		return !(((WallpaperManager) context.getSystemService("wallpaper"))
				.getWallpaperInfo() == null);
	}

	public static int getSpeed() {
		return mSpeed;
	}

	public static void i() {
	}

	private void k()
	 {
	 if (CommonConfig.isLiveWallpaper())
	 return;
	 while (true)
	 {
		 try
		 {
	 WallpaperManager localWallpaperManager=mWallpaperManager;
	 WallpaperInfo info = localWallpaperManager.getWallpaperInfo();
	 if (info == null)
	 break ;
	 new
	 StringBuilder("loadWallpapaer  WallpaperInfo info:").append(info).append("    component:").append(((WallpaperInfo)localObject1).getComponent()).toString();
	 localObject1 = null;
	 if (localObject1 != null)
	 continue;
	 localObject1 = com.censivn.C3DEngine.h.a.a(com.censivn.C3DEngine.a.e(),
	 2130837692);
	 this.e.a((Bitmap)localObject1);
	 this.e.a(Boolean.valueOf(true));
	 }
	 catch (Exception localException)
	 {
	 localException.printStackTrace();
	 this.e.a(Boolean.valueOf(false));
	 }
	 break;
	 label111: Object localObject2 = localWallpaperManager.getDrawable();
	 if ((localObject2 instanceof BitmapDrawable))
	 {
	 localObject2 = ((BitmapDrawable)localObject2).getBitmap();
	 new
	 StringBuilder("loadWallpapaer  bitmap w:").append(((Bitmap)localObject2).getWidth()).append("  h:").append(((Bitmap)localObject2).getHeight()).toString();
	 continue;
	 }
	 localObject2 = null;
	 }
	 }

	// l()
	private boolean isLiveWallpaperMode() {
		WallpaperManager wallpaperManager = mWallpaperManager;
		if (wallpaperManager == null)
			wallpaperManager = (WallpaperManager) SharedData.getContext()
					.getSystemService("wallpaper");
		return wallpaperManager.getWallpaperInfo() != null;
	}

	public final void a(float paramFloat) {
		a(paramFloat, false);
	}

	public final void a(com.tsf.shell.workspace3D.f.a parama) {
		if (parama.k() <= 0)
			b(0.0F);
		else
			b((-1 + parama.k() - parama.v) / (-2 + fm.h().f()));
	}

	public final void b(float paramFloat) {
		if (CommonConfig.getWallpaperMode() == a) {
			if (paramFloat >= 0.0F) {
				if (paramFloat > 1.0F)
					paramFloat = 1.0F;
			} else
				paramFloat = 0.0F;
			this.n = paramFloat;
			if (!CommonConfig.isLiveWallpaper()) {
				if (width < this.h) {
					float f1 = (this.h - width) / 2.0F;
					this.m = false;
					this.l = (int) (f1 - paramFloat * (f1 * 2.0F));
					this.e.a_(true);
					dc.q();
				} else {
					this.e.H().x = 0.0F;
					this.e.a_(false);
				}
			} else {
				this.e.a_(true);
				this.m = false;
				dc.q();
			}
		}
	}

	public final void setMode(int mode) {
		if (mode != SCROLL_MODE) {
			a(0.5F, true);
		} else {
			com.tsf.shell.workspace3D.f.a locala = ShellRenderHost.h().h();
			if (locala.k() <= 0)
				a(0.0F, true);
			else
				a((-1 + locala.k() - locala.v) / (-2 + ShellRenderHost.h().f()),
						true);
		}
	}

	public final WallpaperManager getManager() {
		return mWallpaperManager;
	}

	public final void d() {
		mWallpaperManager = ((WallpaperManager) Home.getInstance()
				.getSystemService("wallpaper"));
		DisplayMetrics localDisplayMetrics = new DisplayMetrics();
		Home.getInstance().getWindowManager().getDefaultDisplay()
				.getMetrics(localDisplayMetrics);
		width = localDisplayMetrics.widthPixels;
		height = localDisplayMetrics.heightPixels;
		// this.e = new dc(this);
		// this.e.H().y = (SharedData.c() / 2);
		k();
	}

	// e()
	public final void e() {
		setMode(CommonConfig.getWallpaperMode());
	}

	// f
	public final void setWallpaperDimension() {
		Display localDisplay = Home.getInstance().getWindowManager()
				.getDefaultDisplay();
		int width;
		int height;
		if (localDisplay.getWidth() >= localDisplay.getHeight()) {
			width = localDisplay.getHeight();
			height = localDisplay.getWidth();
		} else {
			width = localDisplay.getWidth();
			height = localDisplay.getHeight();
		}
		if (mWallpaperManager == null)
			mWallpaperManager = ((WallpaperManager) Home.getInstance()
					.getSystemService("wallpaper"));
		if (CommonConfig.getWallpaperMode() != SCROLL_MODE)
			mWallpaperManager.suggestDesiredDimensions(width, height);
		else
			mWallpaperManager.suggestDesiredDimensions(width * 2, height);
	}

	// g()
	public final void resetWallpaperMode() {
		if (isLiveWallpaperMode() != CommonConfig.isLiveWallpaper())
			Home.getInstance().restartDelayed();
	}

	// public final u h() {
	// return this.e;
	// }

	public void onReceive(Context context, Intent intent) {
		if ((Intent.ACTION_WALLPAPER_CHANGED.equals(intent.getAction()))
				&& (mWallpaperManager != null) && (!this.p)) {
			this.p = true;
			ShellHandler.getInstance().a(new Runnable() {
				@Override
				public void run() {
					RenderRunnable runnable = new RenderRunnable(1,
							RenderRunnable.TYPE_GL_THREAD) {
						public void run() {
							p = false;
							k();
						};
					};
					runnable.mAutoInvalidate = true;
					SharedData.getGLHandler().post(runnable);
				}
			});
		}
	}

}
